//
//  IntervalInfo.m
//  Seizure
//
// Information Gathered over a 1 second interval

#import "IntervalInfo.h"

@implementation IntervalInfo

@synthesize averageMaxValuesX;
@synthesize averageMaxValuesY;
@synthesize averageMaxValuesZ;

@synthesize varianceX, varianceY, varianceZ;
@synthesize freqX, freqY, freqZ;

-(id)initWithIntervalDataForX:(NSMutableArray*)xdata forY:(NSMutableArray*)ydata forZ:(NSMutableArray*)zdata
{
    minHeightThreshold = 0.02;
    
    averageMaxValuesX= 0.0;
    varianceX = 0.0;
    freqX = 0.0;

    averageMaxValuesY= 0.0;
    varianceY= 0.0;
    freqY = 0.0;

    averageMaxValuesZ= 0.0;
    varianceZ= 0.0;
    freqZ = 0.0;
    
    averageMinValuesX=0.0;
    averageMinValuesY=0.0;
    averageMinValuesZ=0.0;

    timesChangedX=0;
    timesChangedY=0;
    timesChangedZ=0;
    
    if( self=[super init] )
    {
        [self parseData:xdata forY:ydata forZ:zdata];
    }
    return self;
}

-(void)parseData:(NSMutableArray*)xdata forY:(NSMutableArray*)ydata forZ:(NSMutableArray*)zdata
{
    [self setXCharacteristics:xdata];
    [self setYCharacteristics:ydata];
    [self setZCharacteristics:zdata];

//    NSLog(@"X Freq : %f  ,  Y Freq : %f   ,  Z Freq : %f", freqX, freqY, freqZ);
//    NSLog(@"AverageMax X: %f\nAverageMax Y: %f\nAverageMax Z: %f\nTimesChanged X: %i\nTimesChanged Y: %i\nTimesChanged Z: %i",
//          averageMaxValuesX,averageMaxValuesY,averageMaxValuesZ,timesChangedX,timesChangedY,timesChangedZ);
}
double highestX = 0;
-(void)setXCharacteristics:(NSMutableArray*)xdata
{
    //First number in the data set.
    NSNumber *previousValue = (NSNumber*)[xdata objectAtIndex:0];
    //Second number in data set.
    NSNumber *currentValue = (NSNumber*)[xdata objectAtIndex:1];
    //Number of data points in the set. For loop
    int dataPointsInSets = xdata.count;
    
    //Keeps track of if the points are rising or falling
    BOOL rising;
    if(currentValue > previousValue)
        rising=YES;
    else
        rising=NO;
    
    previousValue = currentValue;
    //Counts the number of max values and min values for averaging.
    int countOfMaxValues=0;
    int countOfMinValues=0;
    
    // used to help calculate variance
    double sumSquaredMax = 0;
    
    //Go through every point in the xdata
    for(int i=2;i<dataPointsInSets; i++)
    {
        //Get current data point
        currentValue = (NSNumber*)[xdata objectAtIndex:i];
        
        if(!rising && previousValue.doubleValue < currentValue.doubleValue)
        {
            rising=YES;
            timesChangedX++;
            averageMinValuesX += previousValue.doubleValue;
            countOfMinValues++;
        }
        else if(rising && previousValue.doubleValue > currentValue.doubleValue)
        {
            // only count if the max was not a max below 0 (strange case)
            if (previousValue.doubleValue > minHeightThreshold){
                rising=NO;
                timesChangedX++;
                averageMaxValuesX += previousValue.doubleValue;
                sumSquaredMax += previousValue.doubleValue * previousValue.doubleValue;
                countOfMaxValues++;
            }
        }
        
        previousValue = currentValue;
    }
        
    if (timesChangedX > 1)
    {
        freqX = timesChangedX / 2.0;
    }
    
    double mean = averageMaxValuesX/countOfMaxValues;
    //NSLog(@" (%f - %f * %f ) / %d", sumSquaredMax, averageMaxValuesX, mean, (countOfMaxValues-1));
    //                  (squared_sum  - sum * mean ) / n - 1
    varianceX = (sumSquaredMax - averageMaxValuesX*mean)/(countOfMaxValues-1);
//    NSLog(@"Interval Variance of X %f", varianceX);
    averageMaxValuesX = averageMaxValuesX/countOfMaxValues;
    averageMinValuesX = averageMinValuesX/countOfMinValues;
}

double highestY=0.0;
-(void)setYCharacteristics:(NSMutableArray*)ydata
{
    //First number in the data set.
    NSNumber *previousValue = (NSNumber*)[ydata objectAtIndex:0];
    //Second number in data set.
    NSNumber *currentValue = (NSNumber*)[ydata objectAtIndex:1];
    //Number of data points in the set. For loop
    int dataPointsInSets = ydata.count;
    
    //Keeps track of if the points are rising or falling
    BOOL rising;
    if(currentValue > previousValue)
        rising=YES;
    else
        rising=NO;
    
    previousValue = currentValue;
    //Counts the number of max values and min values for averaging.
    int countOfMaxValues=0;
    int countOfMinValues=0;
    
    // used to help calculate variance
    double sumSquaredMax = 0;
    
    //Go through every point in the ydata
    for(int i=2;i<dataPointsInSets; i++)
    {
        //Get current data point
        currentValue = (NSNumber*)[ydata objectAtIndex:i];
        
        if(!rising && previousValue.doubleValue < currentValue.doubleValue)
        {
            rising=YES;
            timesChangedY++;
            averageMinValuesY += previousValue.doubleValue;
            countOfMinValues++;
        }
        else if(rising && previousValue.doubleValue > currentValue.doubleValue)
        {
            //only count if the max was not a max below 0
            if( currentValue.doubleValue > minHeightThreshold )
            {
                rising=NO;
                timesChangedY++;
                averageMaxValuesY += previousValue.doubleValue;
                sumSquaredMax += previousValue.doubleValue * previousValue.doubleValue;
                countOfMaxValues++;
            }
        }
       
        if (timesChangedY > 1)
        {
            freqY = timesChangedY / 2.0;
        }
        previousValue = currentValue;
    }
    
    
    double mean = averageMaxValuesY/countOfMaxValues;
    //NSLog(@" (%f - %f * %f ) / %d", sumSquaredMax,averageMaxValuesY,mean,(countOfMaxValues-1));
    
    varianceY = (sumSquaredMax - averageMaxValuesY*mean)/(countOfMaxValues-1);
//    NSLog(@"Interval Variance of Y %f",varianceY);
    
    averageMaxValuesY = averageMaxValuesY/countOfMaxValues;
    averageMinValuesY = averageMinValuesY/countOfMinValues;
}

double highestZ = 0;
-(void)setZCharacteristics:(NSMutableArray*)zdata
{
    //First number in the data set.
    NSNumber *previousValue = (NSNumber*)[zdata objectAtIndex:0];
    //Second number in data set.
    NSNumber *currentValue = (NSNumber*)[zdata objectAtIndex:1];
    //Number of data points in the set. For loop
    int dataPointsInSets = zdata.count;
    
    //Keeps track of if the points are rising or falling
    BOOL rising;
    if(currentValue > previousValue)
        rising=YES;
    else
        rising=NO;
    
    previousValue = currentValue;
    //Counts the number of max values and min values for averaging.
    int countOfMaxValues=0;
    int countOfMinValues=0;
    
    //used to help calculate variance
    double sumSquaredMax = 0;
    
    //Go through every point in the xdata
    for(int i=2;i<dataPointsInSets; i++)
    {
        //Get current data point
        currentValue = (NSNumber*)[zdata objectAtIndex:i];
        
        if(!rising && previousValue.doubleValue < currentValue.doubleValue)
        {
            rising=YES;
            timesChangedZ++;
            averageMinValuesZ += previousValue.doubleValue;
            countOfMinValues++;
        }
        else if(rising && previousValue.doubleValue > currentValue.doubleValue)
        {
            if(previousValue.doubleValue > minHeightThreshold)
            {
                rising=NO;
                timesChangedZ++;
                averageMaxValuesZ += previousValue.doubleValue;
                sumSquaredMax += previousValue.doubleValue*previousValue.doubleValue;
                countOfMaxValues++;
            }
        }
        if (timesChangedZ > 1)
        {
            freqZ = timesChangedZ / 2.0;
        }
        previousValue = currentValue;
    }
    
    
    double mean = averageMaxValuesZ/countOfMaxValues;
    //NSLog(@" (%f - %f * %f ) / %d", sumSquaredMax, averageMaxValuesZ, mean, (countOfMaxValues-1));
    //                  (squared_sum  - sum * mean ) / n - 1
    varianceZ = (sumSquaredMax - averageMaxValuesZ*mean)/(countOfMaxValues-1);
//    NSLog(@"Interval Variance of Z %f", varianceZ);

    averageMaxValuesZ = averageMaxValuesZ/countOfMaxValues;
    averageMinValuesZ = averageMinValuesZ/countOfMinValues;
}

@end
